/* eslint-disable react/display-name */
import React, { createElement } from 'react';
import { Spin } from 'antd';
import Loadable from 'react-loadable';

const modelNotExisted = (app, model) =>
  // eslint-disable-next-line
  !app._models.some(({ namespace }) => {
    return namespace === model.substring(model.lastIndexOf('/') + 1);
  });

let routerDataCache;

/**
 *  定义动态加载modele方法
 * @param {*} app
 * @param {*} models
 * @param {*} component
 */
const dynamicWrapper = (app, models, component) => {
  models.forEach(model => {
    if (modelNotExisted(app, model)) {
      app.model(require(`../models/${model}.js`).default);
    }
  });

  //新版本  cnpm i -S react-loadable
  // 安装组建
  return Loadable({
    loader: () => {
      routerDataCache = getRouterData(app);
      return component().then(raw => {
        const Component = raw.default || raw;
        return props =>
          createElement(Component, {
            ...props,
            routerData: routerDataCache
          });
      });
    },
    loading: () => {
      //进度
      return <Spin size="large" className="global-spin" />;
    }
  });
};

//定义路由
export const getRouterData = app => {
  const routerConfig = [
    {
      path: '/login',
      component: dynamicWrapper(app, ['login'], () => import('../routes/login/login'))
    },
    {
      path: '/forgotPassword',
      component: dynamicWrapper(app, ['login'], () => import('../routes/forgotPassword/forgotPassword'))
    },
    {
      path: '/',
      component: dynamicWrapper(app, ['example', 'dashboard'], () => import('../layout/Dashboard'))
    },
    {
      path: '/index',
      component: dynamicWrapper(app, ['index'], () => import('../routes/index'))
    },
    {
      path: '/returnRoules',
      component: dynamicWrapper(app, ['returnRoules'], () => import('../routes/returnRoules'))
    },
    {
      path: '/allreturns',
      component: dynamicWrapper(app, ['returnManagement'], () => import('../routes/allreturns'))
    },
    {
      path: '/allorders',
      component: dynamicWrapper(app, ['returnManagement'], () => import('../routes/allreturns'))
    },
    {
      path: '/Pending',
      component: dynamicWrapper(app, ['returnManagement'], () => import('../routes/allreturns'))
    },
    {
      path: '/Approved',
      component: dynamicWrapper(app, ['returnManagement'], () => import('../routes/allreturns'))
    },
    {
      path: '/toBerefunded',
      component: dynamicWrapper(app, ['returnManagement'], () => import('../routes/allreturns'))
    },
    {
      path: '/Rejected',
      component: dynamicWrapper(app, ['returnManagement'], () => import('../routes/allreturns'))
    },
    {
      path: '/Refunded',
      component: dynamicWrapper(app, ['returnManagement'], () => import('../routes/allreturns'))
    },
    {
      path: '/sendMail',
      component: dynamicWrapper(app, ['returnManagement'], () => import('../routes/sendMail'))
    },
    {
      path: '/role',
      component: dynamicWrapper(app, ['role'], () => import('../routes/role/index'))
    },
    {
      path: '/theme',
      component: dynamicWrapper(app, ['theme'], () => import('../routes/theme/index'))
    },
    {
      path: '/access',
      component: dynamicWrapper(app, ['returnManagement', 'access'], () => import('../routes/access/index'))
    },
    {
      path: '/changePassword',
      component: dynamicWrapper(app, ['changePassword'], () => import('../routes/changePassword/index'))
    },

    {
      path: '/ApprovalTime',
      component: dynamicWrapper(app, ['ApprovalTime'], () => import('../routes/ApprovalTime/index'))
    },
    {
      path: '/RefundedSetting',
      component: dynamicWrapper(app, ['RefundedSetting'], () => import('../routes/RefundedSetting/index'))
    }
  ];

  return routerConfig;
};
